package com.classroom.smart.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.classroom.smart.constant.MessageConstant;
import com.classroom.smart.dto.PageDTO;
import com.classroom.smart.mapper.VideoCourseMapper;
import com.classroom.smart.mapper.VideoMapper;
import com.classroom.smart.pojo.Video;
import com.classroom.smart.pojo.VideoCourseExample;
import com.classroom.smart.pojo.VideoExample;
import com.classroom.smart.result.PageResult;
import com.classroom.smart.result.Result;
import com.classroom.smart.service.VideoService;
import com.classroom.smart.utils.VideoUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private VideoCourseMapper videoCourseMapper;

    //查询所有视频信息
    @Transactional(readOnly = true)
    @Override
    public List<Video> findAll() {
        List<Video> videos = videoMapper.selectByExample(null);
        return videos;
    }

    //分页查询
    @Transactional(readOnly = true)
    @Override
    public PageResult findPage(PageDTO pageDTO) {
        Page<Object> page = PageHelper.startPage(pageDTO.getCurrentPage(), pageDTO.getPageSize());
        VideoExample example = new VideoExample();
        String query = pageDTO.getQueryString();
        if (StringUtils.hasLength(query)){
            example.or().andVideoNameEqualTo("%"+query+"%");
        }
        List<Video> list = videoMapper.selectByExample(example);
        return new PageResult(page.getTotal(),list);
    }

    @Transactional
    public String upload(MultipartFile videoFile) throws IOException {
        HashMap hashMap = new HashMap();
        if (videoFile!=null){
            String accessKeyId = "LTAI5tQqjmxN4FdQeweiSvMn";
            String accessKeySecret = "21xncpuDQxuHtSwnCzgNrmCaT0m5Ii";
            String title = videoFile.getOriginalFilename();
            String fileName = videoFile.getOriginalFilename();
            InputStream inputStream = videoFile.getInputStream();

            //1.上传视频的方法
            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
            if (response.isSuccess()) {
                System.out.print("VideoId=" + response.getVideoId() + "\n");
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                System.out.print("ErrorCode=" + response.getCode() + "\n");
                System.out.print("ErrorMessage=" + response.getMessage() + "\n");
            }
            String id = response.getVideoId();
            hashMap.put("id", id);
            hashMap.put("fileName", fileName);
            return fileName;
        }
        return null;
    }

    //新增视频
    @Transactional
    @Override
    public void add(Video video) {
        videoMapper.insertSelective(video);
    }

    //通过视频ID查询信息
    @Transactional(readOnly = true)
    @Override
    public Video viewOne(Integer videoId) {
        Video video = videoMapper.selectByPrimaryKey(videoId);
        return video;
    }

    //修改视频信息
    @Transactional
    @Override
    public void update(Video video) {
        videoMapper.updateByPrimaryKeySelective(video);
    }

    //根据视频ID删除
    @Transactional
    @Override
    public void delete(Integer videoId) {
        VideoCourseExample example = new VideoCourseExample();
        VideoCourseExample.Criteria criteria = example.createCriteria();
        criteria.andVideoIdEqualTo(videoId);
        int count = videoCourseMapper.countByExample(example);
        if (count>0){
            throw new RuntimeException("此视频被占用");
        }
        videoMapper.deleteByPrimaryKey(videoId);
    }
}
